
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;


using Yozeev.BusinessLogic;
using Yozeev.BusinessLogic.Dtos;
using Yozeev.BusinessLogic.DomainService;
using Yozeev.BusinessLogic.Authorization;


namespace Yozeev.BusinessLogic
{
	/// <summary>
	/// ActivityTempDetail应用层服务的接口实现方法  
	///</summary>
	[AbpAuthorize]
	public class ActivityTempDetailAppService : YozeevAppServiceBase, IActivityTempDetailAppService
	{
		private readonly IRepository<ActivityTempDetail, Guid> _entityRepository;

		private readonly IActivityTempDetailManager _entityManager;

		private readonly IRepository<TicketDetail, Guid> _ticketDetailRepository;
		/// <summary>
		/// 构造函数 
		///</summary>
		public ActivityTempDetailAppService(
        IRepository<ActivityTempDetail, Guid> entityRepository
        ,IActivityTempDetailManager entityManager
			, IRepository<TicketDetail, Guid> ticketDetailRepository
		)
        {
            _entityRepository = entityRepository; 
             _entityManager=entityManager;
			_ticketDetailRepository = ticketDetailRepository;

		}


        /// <summary>
        /// 获取ActivityTempDetail的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<ActivityTempDetailListDto>> GetPaged(GetActivityTempDetailsInput input)
		{

		    var query = _entityRepository.GetAll();
			// TODO:根据传入的参数添加过滤条件
            

			var count = await query.CountAsync();

			var entityList = await query
					.OrderBy(input.Sorting).AsNoTracking()
					.PageBy(input)
					.ToListAsync();

			// var entityListDtos = ObjectMapper.Map<List<ActivityTempDetailListDto>>(entityList);
			var entityListDtos =entityList.MapTo<List<ActivityTempDetailListDto>>();

			return new PagedResultDto<ActivityTempDetailListDto>(count,entityListDtos);
		}


		/// <summary>
		/// 通过指定id获取ActivityTempDetailListDto信息
		/// </summary>
		public async Task<ActivityTempDetailListDto> GetById(EntityDto<Guid> input)
		{
			//var entity = await _entityRepository.GetAsync(input.Id);
			var query = from activity in _entityRepository.GetAll().AsNoTracking()
						 .Where(m=>m.Id==input.Id)
						select new
						{
							activity,
							activity.TicketPrice,
							activity.Schedule,
							activity.Schedule.Boat,
							activity.Schedule.Route,
						};
			var list = new List<ActivityTempDetailListDto>();
			query.ToList().ForEach(temp => {
				var _actity = temp.activity.MapTo<ActivityTempDetailListDto>();
				_actity.TicketPrice = temp.TicketPrice;
				_actity.Schedule = temp.Schedule;
				_actity.Schedule.Boat = temp.Boat;
				_actity.Schedule.Route = temp.Route;
				list.Add(_actity);
			});
			// TODO:根据传入的参数添加过滤条件
			var entity = list.FirstOrDefault();
			return entity;
		}

		/// <summary>
		/// 获取编辑 ActivityTempDetail
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task<GetActivityTempDetailForEditOutput> GetForEdit(NullableIdDto<Guid> input)
		{
			var output = new GetActivityTempDetailForEditOutput();
ActivityTempDetailEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _entityRepository.GetAsync(input.Id.Value);

				editDto = entity.MapTo<ActivityTempDetailEditDto>();

				//activityTempDetailEditDto = ObjectMapper.Map<List<activityTempDetailEditDto>>(entity);
			}
			else
			{
				editDto = new ActivityTempDetailEditDto();
			}

			output.ActivityTempDetail = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改ActivityTempDetail的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task CreateOrUpdate(CreateOrUpdateActivityTempDetailInput input)
		{

			if (input.ActivityTempDetail.Id.HasValue)
			{
				await Update(input.ActivityTempDetail);
			}
			else
			{
				await Create(input.ActivityTempDetail);
			}
		}


		/// <summary>
		/// 新增ActivityTempDetail
		/// </summary>
		protected virtual async Task<ActivityTempDetailEditDto> Create(ActivityTempDetailEditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增

            // var entity = ObjectMapper.Map <ActivityTempDetail>(input);
            var entity=input.MapTo<ActivityTempDetail>();
			

			entity = await _entityRepository.InsertAsync(entity);
			return entity.MapTo<ActivityTempDetailEditDto>();
		}

		/// <summary>
		/// 编辑ActivityTempDetail
		/// </summary>
		protected virtual async Task Update(ActivityTempDetailEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新

			var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);

			// ObjectMapper.Map(input, entity);
		    await _entityRepository.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除ActivityTempDetail信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task Delete(EntityDto<Guid> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(input.Id);
		}



		/// <summary>
		/// 批量删除ActivityTempDetail的方法
		/// </summary>
		public async Task BatchDelete(List<Guid> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}


		/// <summary>
		/// 导出ActivityTempDetail为excel表,等待开发。
		/// </summary>
		/// <returns></returns>
		//public async Task<FileDto> GetToExcel()
		//{
		//	var users = await UserManager.Users.ToListAsync();
		//	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
		//	await FillRoleNames(userListDtos);
		//	return _userListExcelExporter.ExportToFile(userListDtos);
		//}

		/// <summary>
		/// 根据预留订单id查询订单明细
		/// </summary>
		/// <param name="activityTempId"></param>
		/// <returns></returns>
		public async Task<List<ActivityTempDetailListDto>> GetDetailListByTempId(Guid activityTempId)
		{
			var query = from activity in _entityRepository.GetAll().AsNoTracking().Where(m => m.ActivityTempId == activityTempId)
						select new
						{
							activity,
							activity.ActivityTemp,
							activity.TicketPrice,
							activity.Schedule,
							activity.Schedule.Boat,
							activity.Schedule.Route,
							activity.Customer,
							//TicketDetail = activity.ActivityTemp.ActivityId == null ? null : 
							//	_ticketDetailRepository.FirstOrDefault(m => m.ActivityId == activity.ActivityTemp.ActivityId && m.CustomerId == activity.CustomerId),
						};
			var list = new List<ActivityTempDetailListDto>();
			query.ToList().ForEach(temp => {
				var _actity = temp.activity.MapTo<ActivityTempDetailListDto>();
				_actity.TicketPrice = temp.TicketPrice;
				_actity.Schedule = temp.Schedule;
				_actity.Schedule.Boat = temp.Boat;
				_actity.Schedule.Route = temp.Route;
				_actity.Customer = temp.Customer;
				_actity.WarePositionName = L(_actity.WarePosition.ToString());
				_actity.TicketDetail = temp.ActivityTemp.ActivityId == null ? new TicketDetail() :
								_ticketDetailRepository.FirstOrDefault(m => m.ActivityId == temp.ActivityTemp.ActivityId && m.CustomerId == temp.activity.CustomerId);
				list.Add(_actity);
			});
			return list;
		}
    }
}


